package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.AlarmQueryPageDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 

     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */

    @Override
    public int updateAlertData(AlertData alertData)
    {

        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        String username = sysUser.getNickName();
        alertData.setProcessorName(username);
        alertData.setStatus(1);
        //修改要把所有的数据都修改所以根据创建时间修改
        //1.0根据id ,查询除出创建时间
        AlertData oldAlertData = this.lambdaQuery()
                .eq(AlertData::getId, alertData.getId()).select(AlertData::getCreateTime)
                .one();

        alertData.setCreateTime(oldAlertData.getCreateTime());
        alertData.setIotId(oldAlertData.getIotId());
        alertData.setFunctionId(oldAlertData.getFunctionId());
        boolean update = this.lambdaUpdate()
                .eq(AlertData::getFunctionId,alertData.getFunctionId())
                .eq(AlertData::getIotId,alertData.getIotId())
                .eq(AlertData::getCreateTime, alertData.getCreateTime()).update(alertData);


        return update? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 分页查询报警数据
     *
     * @param dto
     */

    @Override
    public List<AlertData> pageList(AlarmQueryPageDto dto) {
        List<AlertData> list = this.lambdaQuery()
                .eq(dto.getStatus() != null, AlertData::getStatus, dto.getStatus())
                .eq(dto.getUserId()!=1,AlertData::getUserId, dto.getUserId())
                .eq(dto.getDeviceName() != null, AlertData::getDeviceName, dto.getDeviceName())
                .between(dto.getStartTime() != null && dto.getEndTime() != null, AlertData::getCreateTime, dto.getStartTime(), dto.getEndTime())
                .list();
        list.forEach(alertData -> {
            Long userId = alertData.getUserId();
            SysUser sysUser = sysUserService.selectUserById(userId);
            String userName = sysUser.getNickName();
            alertData.setPreProcessorName(userName);
        });
        return list;
    }
}
